package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/zhaocy/fyne-theme"
)

func main() {
	myApp := app.New()
	myApp.Settings().SetTheme(themex.Themex())
	myWin := myApp.NewWindow("table")

	lb := widget.NewLabel("")

	// 预设的静态数据
	data := [][]string{
		{"唐三", "18", "北京"},
		{"小舞", "16", "上海"},
	}

	//table
	//创建一个空的Table组件
	t := widget.NewTable(nil, nil, nil)

	// 表格的初始化行、列
	t.Length = func() (int, int) {
		return len(data), len(data[0])
	}

	// 创建单元，使用label填充
	t.CreateCell = func() fyne.CanvasObject {
		return widget.NewLabel("table")
	}

	// 更新单元格数据
	t.UpdateCell = func(id widget.TableCellID, template fyne.CanvasObject) {
		if lb, ok := template.(*widget.Label); ok {
			lb.SetText(data[id.Row][id.Col])
		}

	}

	// 选择单元格
	t.OnSelected = func(id widget.TableCellID) {
		lb.SetText(fmt.Sprintf("[%d,%d]", id.Row, id.Col))
	}

	myWin.SetContent(container.NewBorder(lb, nil, nil, nil, t))
	myWin.Resize(fyne.NewSize(550, 450))
	myWin.ShowAndRun()
}
